<?php
class Cltable{	
    public static $emptyTable;
	public $carray;
	public $tableContent;
	public $tdWidth;
	public $isShowKey=false;
	public function setTdWidth($num='200'){
	    $this->tdWidth =$num;	
	}
	public function setPreTdWidth($num='400'){
	    $this->tdpreWidth =$num;	
	}
	const Width="65px";
    public function __construct($arr='empty',$sWidth=''){
		//设置表格开头的html 代码
		self::setEmptyTable();
		//要装入表格的数组
		$this->tableContent=array();
		//检查要装入的数组是不是为空
		$this->checkArray($arr);
		$arrcontent=$this->carray;
		$this->tdWidth='200';
		$this->tdpreWidth =400;
		//$arrcontent 实际上就是经过处理的 $arr; 也就是构造函数的参数1
		//step 1
		$this->mkContent($arrcontent,$sWidth);
	}
	public static function setBg($num){
		return  ($num%2)==0 ? "#fafafa;" : "#fefefe;";
	}
    public function export($arr='empty',$sWidth='200'){
		    // 
			//直接输出 $this->tableContent
		if($arr=='empty'){
			echo Cltable::$emptyTable;	
			foreach( $this->tableContent as $k=>$v){
				echo $v;	
			}
			 echo '</table></div><style>'
			 .' .num { color:#1184CE;font-size:18px; 
			   ;overflow:hidden;border-left:1px solid #ccc ;width:'.$this->tdWidth.'px; }.num b{color:#999;}'
			   .' .pretd{width:'.$this->tdpreWidth.'px;}</style>';
		}else{
			//不为空直接输出
			$this->exportArr($arr,$sWidth);
		}
	}
	public function exportArr($arr,$sWidth=''){
		//设置好内容后就可以用export输出了
	    $this->mkContent($arr,$sWidth);	
		$this->export();
	}
	//把数组变成表格html代码的中间部分，就是tr td格式的字串
	//step 
	public function mkContent($arr,$sWidth){	
	    $i=0;
		$this->tableContent=array();
		foreach( $arr as $k=>$v){	
			$this->tableContent[]="<tr><td  class='num' "
			 ." style='display:block;color:#999;font-size:18px;"
			 ."width:".$sWidth."px;overflow:hidden;'><b>".$k
             ."</b></td><td style='border-color:#ccc;width:4000px;"
			 ." border-left-style:solid; border-left-width:1px;"
			 ."height:12px; padding-left:2px;background-color:"
			 .self::setBg($i)."'>".$v."</td></tr>";
			 $i++;
		}
		
    }
	public function __destruct(){}	
	public function checkArray($arr){
		!is_array($arr) ? $arr=array('Is not array!') : true;
		$this->carray=$arr;
	}
	public static function setEmptyTable(){
	   Cltable::$emptyTable =
	   '<div style="border:1px #ccc solid; margin-top:12px;color:#1184CE;" onDblClick="this.style.display=\'none\';"><table  border="0" cellspacing="0" cellpadding="0" style="background-color:#fff;" class="tablearr" >';	
	}
	
}
final class CltableEx extends Cltable{  
    
    public function mkContent($arr,$sWidth){	
	    $i=0;
		$this->tableContent=array();
		foreach( $arr as $k=>$v){
		
			$this->tableContent[]=
			 $this->arrFormatTrtd($v,$i,$sWidth,$k);
			 $i++;
		}
		
    }
	public function arrFormatTrtd($arr,$i,$sWidth,$k){
		$tmpk=$k;
	    $ret="<tr style='display:block;color:#999;font-size:18px;"
			 ." ".";overflow:hidden;border-left:1px solid #ccc;background-color:"
			 .parent::setBg($i)."'>";
		
		if(is_array($arr)){
			$ii=0;
			
			$ret.="<td><div class='pretd'   >"
			 ." <b>ARR:[".$k."]</b></div></td>";
			 
		    foreach($arr as $key2=>$value){
				
				$keyv= $ii<1 ? $k : $key2;
				if($this->isShowKey){
				$ret.="<td><div>"." <b> " .$key2."</b></td></div>\r\n\r\n";
				}
				$ret.="<td><div class='num' >"._print_r($value,-1,"")."</td></div>";
			$ii++; 
				
				
			}
		}else{
			
			$ret .= "<td><div class='pretd'  "
			 . "><b>STR:[".$tmpk
             ."]</b></td><td></div><div class='num'>".$arr."</div></td>";
			  
        }
		
		$ret.="</tr>";
		return $ret;	
		
	}   
}
?>